package cn.lili.modules.member.serviceimpl;

import cn.hutool.core.date.DateUtil;
import cn.lili.common.security.enums.SceneEnums;
import cn.lili.modules.member.entity.dos.User;
import cn.lili.modules.member.entity.dto.MemberStatisticsSearchParams;
import cn.lili.modules.member.entity.vo.MemberDistributionVO;
import cn.lili.modules.member.mapper.MemberStatisticsMapper;
import cn.lili.modules.member.service.MemberStatisticsService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author paulG
 * @since 2022/6/28
 **/
@Service
public class MemberStatisticsServiceImpl extends ServiceImpl<MemberStatisticsMapper, User> implements MemberStatisticsService {

    @Override
    public long getMemberCount() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enable", true);
        queryWrapper.eq("delete_flag", false);
        queryWrapper.eq("scene", SceneEnums.MEMBER.name());
        return this.baseMapper.customSqlQuery(queryWrapper);
    }

    @Override
    public long todayMemberNum() {
        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.ge("create_time", DateUtil.beginOfDay(new Date()));
        return this.baseMapper.customSqlQuery(queryWrapper);
    }

    @Override
    public long memberCount(Date endTime) {
        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.le("create_time", endTime);
        return this.baseMapper.customSqlQuery(queryWrapper);
    }

    @Override
    public long activeQuantity(Date startTime) {

        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.ge("last_login_date", startTime);
        return this.baseMapper.customSqlQuery(queryWrapper);
    }

    @Override
    public long newlyAdded(MemberStatisticsSearchParams searchParams) {
        QueryWrapper queryWrapper = Wrappers.query();
        queryWrapper.between("create_time", searchParams.getStartTime(), searchParams.getEndTime());
        return this.baseMapper.customSqlQuery(queryWrapper);
    }

    @Override
    public List<MemberDistributionVO> distribution() {
        return this.baseMapper.distribution();
    }
}
